﻿namespace EntityFrameworkDemo.Annotation
{
	using System.ComponentModel.DataAnnotations;

	public class NavigationAttribute : ValidationAttribute
	{
		public int MinLenght
		{
			get;
			set;
		}

		public override bool IsValid(object value)
		{
			if (value == null)
			{
				return false;
			}

            string methodName = "get_Length";
            System.Type t = value.GetType();
            if (t.GetMethod("get_Count") != null)
            {
                methodName = "get_Count";
            }
			
			var count = (int)value
								.GetType()
								.GetMethod(methodName )
								.Invoke(value, null);
			
			return count >= MinLenght;
		}
	}
}
